#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Predefine.h"

Status MakeNode(Link *p, int num){
    *p = (Link) malloc(sizeof(LNode));
    if(*p == NULL)
        return ERROR;
    else{
        (*p)->Num = num;
        return OK;
    }
}

void FreeNode(Link *p){
    if(*p != NULL){
        free(*p);
        *p = NULL;
    }
}

Status IniList(LinkList *L){
    Link p = (Link) malloc(sizeof(LNode));
    if(p != NULL){
        p->next = NULL;
        (*L).head = (*L).tail = p;
        (*L).len = 0;
    }
}

Status DestroyList(LinkList *L){
    ClearList(L);
    FreeNode(&(*L).head);
    (*L).tail = NULL;
    (*L).len = 0;
}

Status ClearList(LinkList *L){
    Link p,q;
    if((*L).head != (*L).tail){
        p = (*L).head;
        for(int i = 0 ; i < (*L).len; i++){
            q = p->next;
            p->Num = NULL;
            p->next = NULL;
            FreeNode(&p);
            p = q;
        }
    }
    (*L).head = (*L).tail;
    (*L).len = 0;
}

Status InsFirst(Link h, Link s){// Link h is the header of Linkedlist
    s->next = h->next;
    h->next = s;
    return OK;
}

Status DelFirst(Link h, Link *q){
    if(h->next != NULL){
        *q = h->next;
        h->next = h->next->next;
        (*q)->next = NULL;
    }
}

Status Append(LinkList *L, Link s){
    
}
